import bygo from "./bygo.weixin.js"

let bodyUrl = "http://localhost:8090/";
let intoUrl = bodyUrl + "in_applet/user/into";
let loginUrl = bodyUrl + "in_applet/user/login";
let phoneNumberLoginUrl = bodyUrl + "/in_applet/user/phone_number/login";

const into = function(res) {

	uni.login({
		success: function(e) {
			uni.setStorageSync("code", e.code)
			console.log("code="+uni.getStorageSync("code"))
		}
	});

	uni.getUserInfo({
		success: function(res) {
			console.log("iv="+res.iv);
			console.log("data="+res.encryptedData);
			uni.setStorageSync("iv", res.iv);
			uni.setStorageSync("encryptedData", res.encryptedData);
		}
	});

	bygo.request({
		url: intoUrl,
		method: "GET",
		data: {
			code: uni.getStorageSync("code"),
			iv: uni.getStorageSync("iv"),
			encryptedData: uni.getStorageSync("encryptedData")
		},
		success: function(res) {
			var data = res.data;
			console.log(data);
			if (data.data.state == -2 || data.data.state == -3) {
				uni.navigateTo({
					url: '/pages/bind_phone_number/bind_phone_number'
				});
				return;
			}
			bygo.request({
				url:loginUrl,
				method:"GET",
				success:function(json){
					var data=json.data;
					if(data.state==2000){
						uni.setStorageSync("userInfo", data.data);						
						uni.switchTab({
							url: '/pages/index/index'
						});
					}
					
				}
			});
			
		}
	});
}

const bindPhoneNumber = function(e) {
	
	uni.login({
		success: function(res) {
			uni.setStorageSync("code", res.code)
			console.log("code="+uni.getStorageSync("code"))
		}
	});

	bygo.request({
		url: phoneNumberLoginUrl,
		method: "GET",
		data: {
			code: uni.getStorageSync("code"),
			iv: e.detail.iv,
			encryptedData: e.detail.encryptedData
		},
		success: function(res) {
			// uni.setStorageSync("userInfo", res.data.data)
			console.log(res.data)
			var data = res.data;
			if (data.state == 2000) {
				uni.setStorageSync("userInfo", data.data);
				uni.switchTab({
					url: '/pages/index/index'
				});
			}
		}
	})

}
export default {
	into,
	bindPhoneNumber
}
